package collection;

/**
 * Created by : Zhong
 * DATE : 2017/3/2
 * Time : 23:06
 * Funtion :
 */
public class PersonCompare implements Comparable{
    public String name;
    public int age;
    public PersonCompare() {
    }
    public PersonCompare(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PersonCompare person = (PersonCompare) o;

        if (age != person.age) return false;
        return name != null ? name.equals(person.name) : person.name == null;

    }
    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    @Override
    public int compareTo(Object o) {

        if (o instanceof PersonCompare){
            PersonCompare personCompare = (PersonCompare) o;
            return this.name.compareTo(personCompare.name);
        }else{
            throw new ClassCastException("转换类型失败！");
        }
    }
}
